package cn.uncode.mq.server;

import org.apache.commons.lang3.StringUtils;

import cn.uncode.mq.server.handlers.FetchRequestHandler;
import cn.uncode.mq.server.handlers.ProducerRequestHandler;

public class UMQ {
	
	private static final String PARAM_KEY_PORT = "-p";

    public static void main(String[] args) {
    	int port = 9999;
    	try {
	        int argsSize = args.length;
	        if (argsSize > 0) {
	        	if(argsSize % 2 != 0){
	                System.out.println("参数数量不对");
	                System.exit(1);
	        	}
	        	int i = 0;
	        	while(i < argsSize){
	        		if(StringUtils.isBlank(args[i]) || StringUtils.isBlank(args[i+1])){
	        			continue;
	        		}
	        		if(PARAM_KEY_PORT.equals(args[i].toLowerCase())){
	        			port = Integer.valueOf(args[i+1]);
	        		}
	        		i += 2;
	        	}
	        }
	        NettyServer nettyServer = new NettyServer();
			nettyServer.start(port);
			nettyServer.registerHandler(RequestHandler.FETCH, new FetchRequestHandler());
			nettyServer.registerHandler(RequestHandler.PRODUCER, new ProducerRequestHandler());
			nettyServer.waitForClose();
		} catch (InterruptedException e) {
			System.err.println(e);
		}
        

    }
}
